package com.hyyt.shopjsp.product.service.imp;

import com.hyyt.shopjsp.product.dao.ShopSpecificationDao;
import com.hyyt.shopjsp.product.pojo.ShopSpecification;
import com.hyyt.shopjsp.product.pojo.ShopSpecificationValue;
import com.hyyt.shopjsp.product.service.IShopSpecificationService;
import com.hyyt.shopjsp.product.service.IShopSpecificationValueService;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/8/6.
 */
@Service
public class ShopSpecificationService implements IShopSpecificationService{
    private static  final Logger logger = Logger.getLogger(IShopSpecificationService.class);

    @Resource
    private ShopSpecificationDao shopSpecificationDAO;
    @Resource
    private IShopSpecificationValueService shopSpecificationValueService;

    /**
     * 保存SHOP_商品规格
     *
     * @param shopSpecification SHOP_商品规格
     * @return 保存后的SHOP_商品规格
     */
    @Override
    public void saveShopSpecification(ShopSpecification shopSpecification) {
        shopSpecificationDAO.insert(shopSpecification);
        List<ShopSpecificationValue> shopSpecificationValueList = shopSpecification.getShopSpecificationValueList();
        if(Utils.collectionIsNotEmpty(shopSpecificationValueList)){
            for(ShopSpecificationValue shopSpecificationValue:shopSpecificationValueList){
                if(Utils.objectIsNotEmpty(shopSpecificationValue.getName()) && Utils.objectIsNotEmpty(shopSpecificationValue.getSort())) {
                    shopSpecificationValue.setSpecificationId(shopSpecification.getSpecificationId());
                    shopSpecificationValueService.saveShopSpecificationValue(shopSpecificationValue);
                }
            }
        }
    }

    /**
     * 更新SHOP_商品
     *
     * @param shopSpecification SHOP_商品规格
     * @return 更新后的SHOP_商品规格
     */
    @Override
    public void updateShopSpecification(ShopSpecification shopSpecification) {
        shopSpecificationDAO.update(shopSpecification);
        List<ShopSpecificationValue> shopSpecificationValueList = shopSpecification.getShopSpecificationValueList();
        if(Utils.collectionIsNotEmpty(shopSpecificationValueList)){
            for(ShopSpecificationValue shopProductAttributeValue:shopSpecificationValueList){
                if(Utils.objectIsNotEmpty(shopProductAttributeValue.getSort()) && Utils.objectIsNotEmpty(shopProductAttributeValue.getName())){
                    if(Utils.objectIsEmpty(shopProductAttributeValue.getSpecificationValueId())){
                        shopProductAttributeValue.setSpecificationId(shopSpecification.getSpecificationId());
                        shopSpecificationValueService.saveShopSpecificationValue(shopProductAttributeValue);
                    }else {
                        shopProductAttributeValue.setSpecificationId(shopSpecification.getSpecificationId());
                        shopSpecificationValueService.updateShopSpecificationValue(shopProductAttributeValue);
                    }
                }
            }
        }
    }

    /**
     * 删除指定ID的SHOP_商品规格
     *
     * @param id SHOP_商品规格ID
     */
    @Override
    public void deleteShopSpecification(Integer id) {
        shopSpecificationDAO.delete(id);
    }

    /**
     * 删除指定ID集合的SHOP_商品规格
     *
     * @param ids SHOP_商品规格ID集合
     */
    public void deleteShopSpecification(Collection<Integer> ids){
        shopSpecificationDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个SHOP_商品规格
     *
     * @param id SHOP_商品规格ID
     * @return SHOP_商品规格
     */
    @Override
    public ShopSpecification loadShopSpecification(Integer id) {
        return shopSpecificationDAO.load(id);
    }

    /**
     * 查询所有的SHOP_商品规格，通常有最多取30000条记录的限制
     *
     * @return 所有的SHOP_商品规格，通常有最多取30000条记录的限制
     */
    @Override
    public List<ShopSpecification> queryAllShopSpecification() {
        return shopSpecificationDAO.queryAll();
    }

    /**
     * 查询SHOP_商品规格
     *
     * @param params 查询参数，为key-value形式
     * @return SHOP_商品规格集合
     */
    @Override
    public List<ShopSpecification> queryShopSpecification(Map<String, Object> params) {
        return shopSpecificationDAO.query(params);
    }

    /**
     * 查询SHOP_商品规格
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品规格集合
     */
    @Override
    public List<ShopSpecification> queryShopSpecification(Map<String, Object> params, int startRow, int rowSize) {
        return shopSpecificationDAO.query(params, startRow, rowSize);
    }

    /**
     * 根据商品分类ID，查询SHOP_商品规格
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品规格集合
     */
    @Override
    public List<Map<String,Object>> queryShopSpecificationByProductTypeId(Map<String, Object> params, int startRow, int rowSize) {
        return shopSpecificationDAO.query("queryShopSpecificationByProductTypeId",params, startRow, rowSize);
    }

    /**
     * 统计SHOP_商品规格
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countShopSpecification(Map<String, Object> params) {
        return shopSpecificationDAO.count(params);
    }

}
